package com.gf.exam.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.exam.dao.CellDao;
import com.gf.exam.mapper.CellMapper;
import com.gf.exam.model.Cell;
import com.gf.exam.model.Timer;

@Service("cellDao")
public class CellDaoImpl extends BaseDaoImpl implements CellDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * insert into cell (examId,number,htmlContent) values (?,?,?)
	 */
	@Value("${cellDao.insertSql}")
	private String insertSql;

	/**
	 * update cell set htmlContent=? where examId=? and number=?
	 */
	@Value("${cellDao.updateSql}")
	private String updateSql;

	/**
	 * delete from cell where examId=? and number=?
	 */
	@Value("${cellDao.deleteBatchSql}")
	private String deleteBatchSql;

	/**
	 * delete from cell where examId=?
	 */
	@Value("${cellDao.deleteSql}")
	private String deleteSql;

	/**
	 * select * from cell where examId=? order by number asc
	 */
	@Value("${cellDao.queryByExamIdSql}")
	private String queryByExamIdSql;

	/**
	 * select * from cell where examId=? and number=?
	 */
	@Value("${cellDao.queryByOneSql}")
	private String queryByOneSql;

	@Override
	public long insert(long examId, int number, String htmlContent,
			String textcontent, Timer timer) {

		return 0;
	}

	@Override
	public int insertBatch(long examId, String[] htmlContents) {
		List<Object[]> argsList = new ArrayList<Object[]>();
		int n = htmlContents.length;
		for (int i = 0; i < n; i++) {
			Object[] obj = new Object[3];
			obj[0] = examId;
			obj[1] = i;
			obj[2] = htmlContents[i];
			argsList.add(obj);
		}

		return this.batchInsertWithIdReturn(insertSql, argsList).size();
	}

	@Override
	public int updateBatch(long examId, String[] htmlContents) {
		List<Object[]> argsList = new ArrayList<Object[]>();
		int n = htmlContents.length;
		for (int i = 0; i < n; i++) {
			Object[] obj = new Object[3];
			obj[0] = htmlContents[i];
			obj[1] = examId;
			obj[2] = i;
			argsList.add(obj);
		}

		return this.batchUpdate(updateSql, argsList).length;
	}

	@Override
	public int deleteBatch(long examId, int[] numbers) {
		List<Object[]> argsList = new ArrayList<Object[]>();
		for (int i = 0; i < numbers.length; i++) {
			Object[] obj = new Object[2];
			obj[0] = examId;
			obj[1] = numbers[i];
			argsList.add(obj);
		}
		return this.batchUpdate(deleteBatchSql, argsList).length;
	}

	@Override
	public void delete(long examId) {
		jdbcTemplate.update(deleteSql, new Object[] { examId });
	}

	@Override
	public List<Cell> queryByExamId(long examId) {
		return jdbcTemplate.query(queryByExamIdSql, CellMapper.getMapper(),
				examId);
	}

	@Override
	public Cell queryByOne(long examId, int number) {
		List<Cell> cells = jdbcTemplate.query(queryByOneSql,
				CellMapper.getMapper(), examId, number);
		if (cells != null && cells.size() >= 1) {
			return cells.get(0);
		}

		return null;
	}

}
